package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.VClassCheckReport;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;

/**
 * 班级测评结果管理处理类
 * 
 * @author zl
 * @since 2018-11-08 15:33
 */

@Controller
@RequestMapping(value = "/sysclassevalresultdetails")
public class SysClassEvalResultDetailsController {

	/**
	 * 班级测评结果详细查询控制类
	 * 
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	// TODO 班级测评结果详细查询控制类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查询测评结果详情", operationClassPath = "controller.SysClassEvalResultDetailsController.getClassEvaResultDetail(Model model, HttpServletRequest req, HttpServletResponse resp)", operationMenu = "班级测评结果管理/班级测评结果详细")
	@RequestMapping(value = "/getclassevaresult")
	public String getClassEvaResultDetail(Model model, HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");

		JSONObject result = new JSONObject();
		Expression exp = new Expression();

		try {
			String schoolid = req.getParameter("schoolid");
			String reporttype = req.getParameter("reporttype");
			String grade = req.getParameter("grade");
			String classid = req.getParameter("classid");

			String currentPage = req.getParameter("page");
			String pageSize = req.getParameter("limit");

			if (currentPage != null && pageSize != null) {
				if (schoolid != null && !schoolid.equals("")) {
					exp.andEqu("schoolid", schoolid, String.class); // 添加schoolid的查询条件
				}
				if (reporttype != null && !reporttype.equals("")) {
					exp.andLike("checkname", reporttype, String.class);
				}
				if (grade != null && !grade.equals("")) {
					exp.andLike("totalclassname", grade, String.class);
				}
				if (classid != null && !classid.equals("") && !classid.equals("null")) {
					exp.andEqu("classid", classid, String.class); // 添加classid的查询条件
				}

				exp.orderByAsc("checktime");

				String strwhere = exp.toString();
				List<VClassCheckReport> listsize = DAOFactory.getCheckReportDAO()
						.getClassCheckReportsByCondition(strwhere);
				List<VClassCheckReport> reportList = DAOFactory.getCheckReportDAO().getClassCheckReportsByCondition(
						strwhere, Integer.parseInt(currentPage), Integer.parseInt(pageSize));
				if (reportList != null && reportList.size() > 0) {
					result.put("code", 0);
					result.put("msg", "班级测评结果查询成功");
					result.put("count", listsize.size());
					JSONArray jsonArray = JSONArray.fromObject(reportList);
					result.element("data", jsonArray);
				} else {
					result.put("code", 1);
					result.put("msg", "无查询结果");
				}
			} else {
				result.put("code", 1);
				result.put("msg", "无查询结果");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 2);
			result.put("msg", "无查询结果");
		}

		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;

	}

}
